{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "676c2ec8",
   "metadata": {},
   "source": [
    "### 避免运行时出现keyerror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "13465796",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "84da4533",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits['apples'] = 10\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b5126a46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'apples' in fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44600532",
   "metadata": {},
   "source": [
    "### 用“in”检查成员关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d28f6ba2",
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1\n",
    "else:\n",
    "    fruits['bananas'] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5e8f0e3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 1}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64457808",
   "metadata": {},
   "source": [
    "### 使用前确保初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "01182fa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1\n",
    "else:\n",
    "    fruits['bananas'] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fba19139",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae3fa786",
   "metadata": {},
   "source": [
    "### 用“not in”替换“in”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1e865b92",
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'pears' not in fruits:\n",
    "    fruits['pears'] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "866505df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 1}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "53e325a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 2}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits.setdefault('pears',0)\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad28df0f",
   "metadata": {},
   "source": [
    "### 使用“setdefault”方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68547389",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels: \")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter, 0)\n",
    "        found[letter] += 1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',found[k],'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbc04eaf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "324cdcb0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
